package com.learn.iot.bean;

import com.learn.iot.handler.MyMapperInvocationHandler;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class MyMapperFactoryBean implements FactoryBean<Object> {

	Class<?> MapperInterface;

	public MyMapperFactoryBean(Class<?> mapperInterface) {
		MapperInterface = mapperInterface;
	}

	@Override
	public Object getObject() throws Exception {
		Class<?>[] clazzs = new Class<?>[]{MapperInterface};
		/**
		 * 通过JDK的动态代理返回MapperInterface的代理对象
		 * MapperInterface会在MyImportBeanDefinitionRegistrar中通过构造方法的参数传过来
		 * MyMapperInvocationHandler中实现对MapperInterface的增强，即mybatis可以通过这个handler处理访问数据库的过程
		 */
		Object o =
				Proxy.newProxyInstance(MyMapperFactoryBean.class.getClassLoader(),clazzs,new MyMapperInvocationHandler());
		return o;
	}

	@Override
	public Class<?> getObjectType() {
		return MapperInterface;
	}
}
